import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {
  getCommonList,
  getIndexAd,
  getNoticeList,
  getOfficialtrainingList,
  getRecommendCourses,
  getSpecialSubjects,
  getSwiper,
  getTeacherList
} from '@/api/index'
import Login from '@/components/Login'
import UserInfo from '@/components/Login/userInfo'

var that
export default {
  name: 'index',
  components: {
    Pagination,
    Login,
    UserInfo
  },
  data() {
    return {
      swiper1: {
        // 设置分页器
        pagination: {
          el: '.swiper-pagination1',
          // 设置点击可切换
          clickable: true
        },
        // 设置自动轮播
        autoplay: {
          delay: 3000 // 5秒切换一次
        },
        // 设置轮播可循环
        loop: false,
        on: {
          click(e) {
            that.swiperList.forEach((item) => {
              if (item.id == e.target.dataset.id) {
                that.toSwiper(item)
              }
            })
          }
        }
      },
      swiper2: {
        // 设置分页器
        pagination: {
          el: '.swiper-pagination1',
          // 设置点击可切换
          clickable: true
        },
        // 设置自动轮播
        autoplay: {
          delay: 3000 // 5秒切换一次
        },
        // 设置轮播可循环
        loop: true,
        on: {
          click(e) {
            that.swiperList.forEach((item) => {
              if (item.id == e.target.dataset.id) {
                that.toSwiper(item)
              }
            })
          }
        }
      },
      swiperOption: {
        slidesPerView: 5,
        spaceBetween: 30,
        // 设置分页器
        pagination: {
          el: '.swiper-pagination',
          // 设置点击可切换
          clickable: true
        },
        // 设置自动轮播
        autoplay: {
          delay: 3000 // 5秒切换一次
        },
        // 设置轮播可循环
        loop: false
      },
      swiperTeacherOption: {
        slidesPerView: 4,
        spaceBetween: 40,
        // 设置分页器
        pagination: {
          el: '.swiper-pagination',
          // 设置点击可切换
          clickable: true
        },
        // 设置前进后退按钮
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        },
        // 设置轮播可循环
        loop: false
      },
      swiperList: [],
      recommendCourseList: [],
      noticeList: [],
      officialtrainingList: [],
      teacherList: [],
      specialList: [],
      commonList: [],
      adsList: [],
      pcImgList: [],
      pcImgFlag: false
    }
  },
  computed: {
    swiperListOption() {
      var len = this.swiperList.length
      if (len == 1) {
        return this.swiper1
      } else {
        return this.swiper2
      }
    }
  },

  methods: {
    onSwiper() {

    },
    onSlideChange() {

    },
    toCertificate() {
      if (this.$store.state.app.footer.certificateExternalLinks) {
        window.open(this.$store.state.app.footer.certificateExternalLinks, '_blank')
      } else {
        window.open(this.$router.resolve({
          path: '/certificate'
        }).href, '_blank')
      }

    },
    toDetail(id, NOTICETYPE) {
      this.$router.push({
        path: '/notice/detail',
        query: {
          NOTICEID: id,
          NOTICETYPE
        }
      })
    },
    showTcourse(id) {
      this.$router.push({
        path: '/special-info',
        query: {
          trainingId: id
        }
      })
    },
    toClass(id) {
      this.$router.push({
        path: '/course/singleCourseInfo',
        query: {
          courseId: id
        }
      })
    },
    getSwiperList() {
      var data = {
        adType: 'PC',
        appId: '',
        limit: '',
        time: ''
      }
      getSwiper(data).then((response) => {
        this.swiperList = response.data
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },
    getAds() {
      var data = {
        adType: 'PC',
        adPosition: '1',
        appId: '',
        limit: '2',
        time: ''
      }
      getIndexAd(data).then((response) => {
        this.adsList = response.data
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },

    getPiaoChuangAds() {
      var data = {
        adType: 'PC',
        adPosition: '10',
        appId: '',
        limit: '1',
        time: ''
      }
      getIndexAd(data).then((response) => {
        this.pcImgList = response.data
        console.log(this.pcImgList)
        if (this.pcImgList.length !== 0) {
          this.pcImgFlag = true
          var ad = new AdMove('Div2')
          ad.Run()
        }
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },

    getRecommendCoursesList() {
      var limit = 8
      getRecommendCourses(limit).then((response) => {
        this.recommendCourseList = response.data
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },
    getNotice() {
      var limit = 5
      getNoticeList(limit).then((response) => {
        this.noticeList = response.data
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },
    getOfficialtraining() {
      var limit = 5
      getOfficialtrainingList(limit).then((response) => {
        this.officialtrainingList = response.data
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },

    getTeacher() {
      var limit = 14
      getTeacherList(limit).then((response) => {
        this.teacherList = response.data
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },
    getSpecial() {
      var limit = 14
      getSpecialSubjects(limit).then((response) => {
        this.specialList = response.data
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },
    getCommon() {
      getCommonList(5).then((response) => {
        this.commonList = response.data
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },
    toCommonQuestion(id) {

    },
    toHelpCenter(type) {
      this.$router.push({
        path: '/help-center',
        query: {
          type
        }
      })
    },
    toSwiper(swiper) {
      if (swiper.isLogin == 0) { //必须登录
        this.openUrl(swiper.adOpenWay, swiper.adUrl)
      } else {
        if (this.$store.state.user.token) {
          this.openUrl(swiper.adOpenWay, swiper.adUrl)
        } else {
          this.$alert('请先登录', '提示', {
            confirmButtonText: '确定'
          })
        }
      }
    },
    openUrl(adOpenWay, adUrl) {
      if (adOpenWay === '2') { //新页面吧
        window.open(adUrl, '_blank')
      } else if (adOpenWay === '3') {
        window.open(adUrl, '_self')
      }
    }
  },
  mounted() {
    that = this

    this.getSwiperList()
    this.getRecommendCoursesList()
    this.getNotice()
    this.getOfficialtraining()
    this.getAds()
    this.getPiaoChuangAds()

    /* this.getTeacher();
     this.getSpecial();
     this.getCommon();*/

    this.$store.dispatch('app/getFooter').then(() => {
      document.title = this.$store.getters.footer.appTitle
    })
  }
}
